package com.app.micaihu.base;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * dialog 基类
 */
@Deprecated
public abstract class BaseOldDialog extends DialogFragment {
    protected final String TAG = getClass().getSimpleName();

    protected View thisView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDialogStyle();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 设置圆角需要
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
        }
        setCancelable(getCancelable());
        thisView = inflater.inflate(getLayoutId(), container, false);
        init(getArguments());
        setData();
        setListener();
        return thisView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            // 重置宽高
            Window window = getDialog().getWindow();
            if (window != null) {
                window.setLayout(getWidth(), getHeight());
            }
        }
    }

    /**
     * 显示Dialog
     */
    public void show(FragmentManager fragmentManager) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment pre = fragmentManager.findFragmentByTag(TAG);
        if (pre != null) {
            transaction.remove(pre);
        }
//       onSaveInstance后commit无法提交
        try {
            show(transaction, TAG);
        } catch (IllegalStateException e) {
            //  容错处理,不做操作
        }
    }

    /**
     * 显示Dialog
     */
    public void show(@NonNull FragmentActivity activity) {
        show(activity.getSupportFragmentManager());
    }

    /**
     * 避免莫名其妙的空指针
     * onSaveInstance后commit无法提交
     */
    @Override
    public void dismiss() {
        if (getFragmentManager() == null) {
            return;
        }
        dismissAllowingStateLoss();
    }

    protected abstract int getLayoutId();

    protected abstract void init(Bundle bundle);

    protected abstract void setData();

    protected abstract void setListener();

    /**
     * override
     * 配置对话框风格  背景不再变黑
     */
    protected void setDialogStyle() {
//        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Theme_BaseDialogTheme);
    }

    /**
     * override
     * 配置点击对话框外部是否消失
     */
    protected boolean getCanceledOnTouchOutside() {
        return false;
    }

    /**
     * override
     * 配置返回键是否可以关闭对话框
     */
    protected boolean getCancelable() {
        return true;
    }

    /**
     * override
     * 配置宽度
     */
    protected int getWidth() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    /**
     * override
     * 配置高度
     */
    protected int getHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

}
